# Xern build script. Based in example from Ogre wiki.

cmake_minimum_required(VERSION 2.6)
project(Xern)

set(XERN_PLUGIN_TYPE STATIC)

# Check the build type
if (CMAKE_BUILD_TYPE STREQUAL "")
	# CMake defaults to leaving CMAKE_BUILD_TYPE empty. This screws up
	# differentiation between debug and release builds.
	set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build, options are: None (CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif ()

set(XERN_PLUGIN_TYPE MODULE)
 
if(WIN32)
	set(CMAKE_MODULE_PATH "$ENV{OGRE_HOME}/CMake/;${CMAKE_MODULE_PATH}")
	#set(COMMON_LIBRARIES)
endif(WIN32)

if(UNIX)
	set(CMAKE_MODULE_PATH "/usr/local/lib/OGRE/cmake/;${CMAKE_MODULE_PATH}")
	set(COMMON_LIBRARIES dl pthread)
endif(UNIX)

if (CMAKE_BUILD_TYPE STREQUAL "")
  # CMake defaults to leaving CMAKE_BUILD_TYPE empty. This screws up
  # differentiation between debug and release builds.
  set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build, options are: None (CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif ()

set(CMAKE_DEBUG_POSTFIX "_d")
 
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/dist")
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/;${CMAKE_MODULE_PATH}")

# Perform platform checks
include(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
include(${CMAKE_ROOT}/Modules/CheckIncludeFileCXX.cmake)
include(${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
include(${CMAKE_ROOT}/Modules/CheckLibraryExists.cmake)
CHECK_INCLUDE_FILE("inttypes.h" HAVE_INTTYPES_H)
CHECK_INCLUDE_FILE("memory.h" HAVE_MEMORY_H)
CHECK_INCLUDE_FILE("stdint.h" HAVE_STDINT_H)
CHECK_INCLUDE_FILE("stdlib.h" HAVE_STDLIB_H)
CHECK_INCLUDE_FILE("strings.h" HAVE_STRINGS_H)
CHECK_INCLUDE_FILE("string.h" HAVE_STRING_H)
CHECK_INCLUDE_FILE("sys/stat.h" HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILE("sys/types.h" HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILE("unistd.h" HAVE_UNISTD_H)

# Check for crypto++
CHECK_INCLUDE_FILE_CXX("crypto++/cryptlib.h" HAVE_CRYPTO_CRYPTLIB_H)
#CHECK_LIBRARY_EXISTS(crypto++ main "" HAVE_CRYPTOPP)

if(HAVE_CRYPTO_CRYPTLIB_H)# AND HAVE_CRYPTOPP)
	set(CRYPTOPP_FOUND TRUE)
endif()

if(NOT CRYPTOPP_FOUND)
	message(SEND_ERROR "Failed to found crypto++.")
endif()

# Check for openjpeg
CHECK_INCLUDE_FILE("openjpeg.h" HAVE_OPENJPEG_H)
CHECK_LIBRARY_EXISTS(openjpeg opj_create_decompress "" HAVE_OPENJPEG)

if(HAVE_OPENJPEG_H AND HAVE_OPENJPEG)
	set(OPENJPEG_FOUND TRUE)
endif()

# Check for ogg and vorbis
CHECK_INCLUDE_FILE("ogg/ogg.h" HAVE_OGG_OGG_H)
CHECK_INCLUDE_FILE("vorbis/codec.h" HAVE_VORBIS_CODEC_H)
CHECK_INCLUDE_FILE("vorbis/vorbisenc.h" HAVE_VORBIS_VORBISENC_H)
CHECK_INCLUDE_FILE("vorbis/vorbisfile.h" HAVE_VORBIS_VORBISFILE_H)
CHECK_LIBRARY_EXISTS(ogg ogg_stream_init "" HAVE_OGG)
CHECK_LIBRARY_EXISTS(vorbis vorbis_info_init "" HAVE_VORBIS)
CHECK_LIBRARY_EXISTS(vorbisenc vorbis_encode_init "" HAVE_VORBISENC)
CHECK_LIBRARY_EXISTS(vorbisfile ov_open "" HAVE_VORBISFILE)

if(HAVE_OGG AND HAVE_OGG_OGG_H)
	set(OGG_FOUND TRUE)
endif()

if(HAVE_VORBIS_CODEC_H AND HAVE_VORBIS)
	set(VORBIS_FOUND TRUE)
endif()

if(HAVE_VORBIS_VORBISENC_H AND HAVE_VORBISENC)
	set(VORBISENC_FOUND TRUE)
endif()

if(HAVE_VORBIS_VORBISFILE_H AND HAVE_VORBISFILE)
	set(VORBISFILE_FOUND TRUE)
endif()

# Find bison
find_package(BISON REQUIRED)

# Find Ogre.
find_package(OGRE REQUIRED)

# Check Ogre version.
if(NOT "${OGRE_VERSION_NAME}" STREQUAL "Cthugha")
  message(SEND_ERROR "You need Ogre 1.7 Cthugha to build this.")
endif()

# Find OIS
find_package(OIS REQUIRED)
 
if(NOT OIS_FOUND)
	message(SEND_ERROR "Failed to find OIS.")
endif()

# Find boost.
set(OGRE_BOOST_COMPONENTS thread date_time system)
find_package(Boost COMPONENTS ${OGRE_BOOST_COMPONENTS})
if (NOT Boost_FOUND)
    # Try again with the other type of libs
	message("boost try again")
	set(Boost_USE_STATIC_LIBS NOT ${Boost_USE_STATIC_LIBS})
	find_package(Boost COMPONENTS ${OGRE_BOOST_COMPONENTS} QUIET)
endif()

# Find bullet
find_package(Bullet REQUIRED)

if(NOT BULLET_FOUND)
	message(SEND_ERROR "Failed to find Bullet")
endif()

# Find openal
find_package(OpenAL REQUIRED)

if(NOT OPENAL_FOUND)
	message(SEND_ERROR "Failed to find OpenAL")
endif()

# Find xml-rpc
find_package(XMLRPC COMPONENTS c++2)

# Set up referencing of Boost
include_directories(${Boost_INCLUDE_DIR})
add_definitions(-DBOOST_ALL_NO_LIB)
set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${Boost_LIBRARIES})
set(COMMON_LIBRARIES ${Boost_LIBRARIES} ${COMMON_LIBRARIES})

# Create directories.
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/dist/plugins)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/dist/media DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/dist/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/dist/cfg DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/dist/)

# Set Ogre include directories.
include_directories(
	${OIS_INCLUDE_DIRS}
	${OGRE_INCLUDE_DIRS}
)

# Add openal include directories.
include_directories(${OPENAL_INCLUDE_DIR})

# Add the current directory.
include_directories(.)

# Add the local include directory.
include_directories(include)

# Add squirrel include directory
include_directories(include/squirrel)

# Add MyGUI include directory
include_directories(
	thirdparty/MyGUI3.0/MyGUIEngine/include
	thirdparty/MyGUI3.0/Platforms/Ogre/OgrePlatform/include
)

# Add pure data include directory.
include_directories(thirdparty/pd-0.42-6/include)

if("${XERN_PLUGIN_TYPE}" STREQUAL "STATIC")
	set(SERVER_PLUGINS
		BulletPhysics
		SquirrelScript
		ServerModule
	)
	
	set(SERVER_PLUGINS_DEPS
		Squirrel
		SqStdLib
	)
	
	set(CLIENT_PLUGINS
		${SERVER_PLUGINS}
		OgreRenderer
		OISInput
		MyGUISquirrel
		MyGUISystem
		ClientModule
	)
	
	set(CLIENT_PLUGINS_DEPS
		${SERVER_PLUGINS_DEPS}
		
		MyGUIEngine
		MyGUI.OgrePlatform
		${OGRE_LIBRARIES}
		${OIS_LIBRARIES}
	)
endif()
message(STATUS "Xern plugins build type ${XERN_PLUGIN_TYPE}")

# Build thirdparty dependencies.
add_subdirectory(thirdparty)

# Add cmake subdirectories.
add_subdirectory(apps)

add_subdirectory(include)

add_subdirectory(libs)

add_subdirectory(plugins)


